define(['jquery',
        'mustache',
        'module/home/moduleFactory',
        'module/home/customEvent',
        'module/home/showUpdateBox',
        'module/home/more',
        'module/leftMainMenu'
    ],
    function ($,Mustache, moduleFactory, customEvent, updateBox, more, leftMainMenu) {
        // we need to create update box also.


        var MainUpdate = function (){

            // load things for update in main arena.
            var thisAlways = this, pageDS = {maxLastSeenId: 0, minLastSeenId: 0, pageSize: 10},
                template = require('module/template/homeTemplate');

            // bind document with event of appending and pre pending.


            this.initialize = function(){
                $('.main-content').empty();
                $('.main-content').append(template['home-home']);
                pageDS.maxLastSeenId = 0;
                pageDS.minLastSeenId = 0;
                updateBox($("#updateInput"), {updateType : '1'});
            };

            this.takeUpdate = function (append) {

                if (append) {

                    var dynaClass = 'js_'+customEvent.UPDATE_PAGE, mainUpdate = $('.main-content').find('.update-area'),
                        loading = $(template['home-loading']);
                    mainUpdate.addClass(dynaClass);
                    mainUpdate.append(loading);
                    /**
                     * update on home page.
                     */
                    $.ajax({
                        url: '/user/getUpdate', data: {
                            lastSeenId: pageDS.minLastSeenId,
                            limit: pageDS.pageSize,
                            after: ((pageDS.minLastSeenId == 0) ? true : false)
                        }, dataType: "json"
                    }).done(function (data) {
                        $.each(data, function (index, value) {
                            $('.'+dynaClass).append(moduleFactory(value)).slideDown();
                            if (pageDS.maxLastSeenId < value.upid) {
                                pageDS.maxLastSeenId = value.upid;
                            }
                            pageDS.minLastSeenId = value.upid;
                        });
                        more(data.length, pageDS.pageSize, customEvent.UPDATE_PAGE);
                        loading.remove();
                    });

                } else {

                    /**
                     * update on home page.
                     */
                    $.ajax({
                        url: '/user/getUpdate', data: {
                            lastSeenId: pageDS.maxLastSeenId,
                            limit: pageDS.pageSize,
                            after: true
                        }, dataType: "json"
                    }).done(function (data) {
                        $.each(data, function (index, value) {
                            $(".main-content").children('.update-area').prepend(moduleFactory(value)).slideDown();
                            if (pageDS.maxLastSeenId < value.upid) {
                                pageDS.maxLastSeenId = value.upid;
                            }
                        });
                    });
                }
            };

            this.renderLoggedUser = function(userArray){

                $.each(userArray, function(index, user){
                    if(user.remove){
                        $('.right-content').find('.list-group-item').
                            children("a[data-user='" + user.u.id +"']").parent().remove();
                    } else if(user.add){
                        var chatDom = $(Mustache.render(template['people-chat-li'], user.u  ));
                        $('.right-content').append(chatDom);
                        require(['module/chat/chat'], function(chat){
                            chat.addEvent(chatDom);
                        });
                    }

                    if( $('.right-content').find('.list-group-item').length > 0){
                        $('.right-content').prepend($('<li class="list-group-item head"> Current users </li>'))
                    } else {
                        $('.right-content').find('.head').remove();
                    }
                });

            }

        };

       var mainUpdate = new MainUpdate();

        customEvent.bindEvent(customEvent.UPDATE_PAGE, function(event){
            if(event.initialize){
                mainUpdate.initialize();
            }
            mainUpdate.takeUpdate(event.append);
        });


        customEvent.bindEvent(customEvent.GET_LOGGED_USERS, function(event){
            mainUpdate.renderLoggedUser(event.loggedInUser);
        });


        return {

            startUpdate : function(){
                $.event.trigger({
                    type: customEvent.UPDATE_PAGE,
                    append: true,
                    initialize : true
                });
            },
            startMainMenu : function(){
                leftMainMenu();
            },

            initialize : function(){
                this.startMainMenu();
                this.startUpdate();
            }
        };
    });